<?php
/**
 * 项目管理控制器
 * User: zero
 * Date: 2015/9/20
 * Time: 21:51
 */
namespace Addons\UserCenter\Controller;
use Home\Controller\AddonsController;

class ProjectController extends AddonsController{
    function _initialize() {
        //粉丝登录
        if(session('is_follow_login') == 1){
            if(empty(session('user_auth')) && empty(session('oauth2'))){
                if(isset($_GET['code'])){
                    getUserInfo();
                }else{
                    wx_login(GetCurUrl());
                }
            }
            C ( 'EDITOR_UPLOAD.rootPath', './Uploads/Editor/' . get_token () . '/' );
        }

    }
    /**
     * 获取项目列表
     */
    public function lists(){
        $status = I('status', '');
        cookie('lastOpen'.$status, time(), ['expire'=>(60*60*24*360)]);

        $openid = $map ['openid'] = session('oauth2.openid');
        $token = $map ['token'] = get_token();
        if($openid == -1 || $token == -1){
            $this->error('非法访问');
        }
        $list_data['meate_title'] = '项目列表';
        if($_GET['status'] === '0' || !empty(I('get.status'))){
            $map['status'] = I('get.status');
            $sub_title = C('PROJECT_STATUS')[$map['status']];
        }
        $list_data['meate_title'] = $sub_title . '项目列表';
        $page = I ( 'p', 1, 'intval' );
        $row = isset ( $_REQUEST ['list_row'] ) ? intval ( $_REQUEST ['list_row'] ) : 20;
        /* 查询记录总数 */
        $count = M ( 'project' )->where ( $map )->count ();
        // 分页
        if ($count > $row) {
            $Page = new \Think\Page ( $count, $row );
            $Page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $Page->show ();
        }

        $list_data['list_data'] = M('project')->where($map)->page($page, $row)->order('create_time desc')->select();
        unset($map['status']);
        $list_data['map'] = $map;
        $this->assign($list_data);
        $this->display();
    }

    /**
     * 添加项目
     */
    public function add(){
        $openid = $map ['openid'] = session('oauth2.openid');
        $token = $map ['token'] = get_token();
        if($openid == -1 || $token == -1){
            $this->error('非法访问');
        }
        $html = 'moblieForm';
        $model = $this->getModel ( 'project' );
        $fields = get_model_attribute ( $model ['id'] );
        $fieldArr = array('name','address','area', 'amount', 'heat', 'electric', 'developer', 'remark'); //headimgurl
        foreach($fields[1] as $k=>$vo){
            if(!in_array($vo['name'], $fieldArr))
                unset($fields[1][$k]);
        }
        $config = getAddonConfig ( 'UserCenter' );
        $this->assign('backbround', $config['projectBackbround']);
        $this->assign('map', $map);
        $this->assign ( 'fields', $fields );
        $this->meta_title = '提交项目';

        $this->assign('post_url', U('update'));

        $this->display($html);
    }
    // 通用插件的编辑模型
    public function editProject( $id = 0) {
        $id = I('id');
        //获取原始数据
        $Project = M('Project');
        $info = $Project->where(['id'=>$id])->find();
        if(empty($info)){
            $this->error('数据不存在');
        }
        if((NOW_TIME - $info['create_time']) > 1800){
            $this->error('只能在提交半个小时内，修改');
        }
        $model = $this->getModel ( 'Project' );
        if(IS_POST){
            $Model = D ( parse_name ( get_table_name ( $model ['id'] ), 1 ) );
            // 获取模型的字段信息
            $Model = $this->checkAttr ( $Model, $model ['id'] );
            if ($Model->create () && $Model->save ()) {
                $this->_saveKeyword ( $model, $id );

                $this->success ( '保存' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ) );
            } else {
                $this->error ( $Model->getError () );
            }
        }else{
            $fields = get_model_attribute ( $model ['id'] );
            $fieldArr = array('id','name','address','area','amount','heat','electric','developer','remark'); //headimgurl
            foreach($fields[1] as $k=>$vo){
                if(!in_array($vo['name'], $fieldArr))
                    unset($fields[1][$k]);
            }
            $this->assign('post_url', U('editProject'));
            $this->assign ( 'fields', $fields );
            $this->assign ( 'data', $info );
            $this->meta_title = '编辑' . $model ['title'];
            $this->display ( 'moblieForm' );

        }
    }

    /**
     * 处理更新和添加
     */
    public function update(){
        $model = $this->getModel( 'project' );
        $Model = D( parse_name( get_table_name( $model ['id'] ), 1 ) );
        $map = [
            'openid' => session('oauth2.openid'),
            'token' => get_token(),
        ];
        if($map['openid'] == -1 || $map['token'] == -1){
            $this->error('非法访问');
        }
        $_POST = array_merge($_POST, $map);
        // 获取模型的字段信息
        $Model = $this->checkAttr( $Model, $model ['id'] );
        if ($data = $Model->create()) {
            if($data['id']){
                $where['id'] = $data['id'];
                $res = $Model->where ( $where )->save ();
            }else{
                $res = $Model->add();
            }
            if($res !== false){
                $this->success('成功', addons_url('UserCenter://UserCenter/userCenter'), $map);
            }else{
                $this->error($Model->getError());
            }
        }else{
            $this->error($Model->getError());
        }
    }
}